var brandList, pbcList;

$(function () {

	//查询条件重置
    $("#search_reset").click(function(){
    	$("input").val("");
        $("select").val("");
    });

    //录入商品
    $("[name='btn_save_new_product']").click(function () {
        var form = window.form_new_product;
        var name = form.name.value;
        var number = form.number.value;
        var barcode = form.barcode.value;
        var price = form.price.value;
        var brandId = form.brandId.value;
        var state = form.state.value;
        var pbcId = form.pbcId.value;
        var pictures = "";
        $("#ctn_new_product_pics img").each(function () {
            pictures += $(this).data("picuri") + ",";
        });
        if (pictures.length > 0) {
            pictures = pictures.substring(0, pictures.length - 1);
        }

        if(!name){
        	bootbox.balert("请输入产品名称");
            return;
        }
        if(!number){
        	bootbox.balert("请输入产品编号");
            return;
        }
        if(!barcode){
        	bootbox.balert("请输入产品条码");
            return;
        }
        if (isNaN(price)) {
            bootbox.balert("价格输入有误");
            return;
        }
        if(!brandId){
        	bootbox.balert("请选择产品品牌");
            return;
        }
        if(!pbcId){
        	bootbox.balert("请选择产品规格");
            return;
        }

        var product = {name: name, number: number, barcode: barcode, price: price, state: state, brandId: brandId, pbcId: pbcId, pictures: pictures};
        $.postJsonBody("/product", product, function (re) {
            // tipper.success("已保存，请刷新页面");
            // $("#md_new_brand").modal("hide");
            window.location.href=window.location.href; 
            window.location.reload; 
        })
    });

    //编辑商品
    $("[name='btn_save_edit_product']").click(function () {
        var form = window.form_edit_product;
        var id = form.id.value;
        var name = form.name.value;
        var number = form.number.value;
        var barcode = form.barcode.value;
        var price = form.price.value;
        var brandId = form.brandId.value;
        var state = form.state.value;
        var pbcId = form.pbcId.value;
        var pictures = "";
        $("#ctn_edit_product_pics img").each(function () {
            pictures += $(this).data("picuri") + ",";
        });
        if (pictures.length > 0) {
            pictures = pictures.substring(0, pictures.length - 1);
        }

        if(!name){
        	bootbox.balert("请输入产品名称");
            return;
        }
        if(!number){
        	bootbox.balert("请输入产品编号");
            return;
        }
        if(!barcode){
        	bootbox.balert("请输入产品条码");
            return;
        }
        if (isNaN(price)) {
            bootbox.balert("价格输入有误");
            return;
        }
        if(!brandId){
        	bootbox.balert("请选择产品品牌");
            return;
        }
        if(!pbcId){
        	bootbox.balert("请选择产品规格");
            return;
        }

        var product = {id: id, name: name, number: number, barcode: barcode, price: price, state: state, brandId: brandId, pbcId: pbcId, pictures: pictures};
        $.putJsonBody("/product/" + number, product, function (re) {
            // tipper.success("已保存，请刷新页面");
            // $("#md_new_brand").modal("hide");
        	   window.location.href=window.location.href; 
               window.location.reload; 
        })
    });

    //删除商品
    $("[name='btn_delete_product']").click(function () {
        var productNumber = $(this).data("product-number");
        bootbox.bconfirm("确定要删除这个商品吗？", function (re) {
            if (re) {
                $.delete("/product/" + productNumber, function (re) {
                    if (re.data) {
                        // tipper.success("删除成功，请刷新页面");
                    	   window.location.href=window.location.href; 
                           window.location.reload; 
                    } else {
                        tipper.danger("删除失败，请重试");
                    }
                });
            }
        });
    });


    //上传图片
    $("[name='btn_new_product_upload_pic']").click(function () {
        var uploaded_count = $("#ctn_new_product_pics").find("img").length;
        if (uploaded_count < 3) {
            $("#ipt_new_product_upload_pic").click();
        } else {
            bootbox.balert("每款产品最多只能上传3张图片");
            return;
        }
    });
    $('#ipt_new_product_upload_pic').fileupload({
        dataType: 'json',
        url: "/upload/pic?type=1",
        done: function (e, data) {
            var res = data.jqXHR.responseJSON;
            var $imgEle = $("<img src='" + PicDomain + res.data + "' data-picuri='" + res.data + "'/>");
            $imgEle.click(function () {
                $(this).remove();
            });
            $("#ctn_new_product_pics").append($imgEle);
        }
    });

    $("[name='btn_edit_product_upload_pic']").click(function () {
        var uploaded_count = $("#ctn_edit_product_pics").find("img").length;
        if (uploaded_count < 3) {
            $("#ipt_edit_product_upload_pic").click();
        } else {
            bootbox.balert("每款产品最多只能上传3张图片");
            return;
        }
    });
    $('#ipt_edit_product_upload_pic').fileupload({
        dataType: 'json',
        url: "/upload/pic?type=1",
        done: function (e, data) {
            var res = data.jqXHR.responseJSON;
            var $imgEle = $("<img src='" + PicDomain + res.data + "' data-picuri='" + res.data + "'/>");
            $imgEle.click(function () {
                $(this).remove();
            });
            $("#ctn_edit_product_pics").append($imgEle);
        }
    });


    //加载异步数据
    $.get("/brand?state=1", function (re) {
        var brandList = re.data;
        var form = window.form_new_product;
        var brandOptions = "";
        for (var i in brandList) {
            var brand = brandList[i];
            brandOptions += "<option value='" + brand.id + "'>" + brand.name + "</option>";
        }
        $(window.form_new_product.brandId).append(brandOptions);
        $(window.form_edit_product.brandId).append(brandOptions);
    });
    $.get("/pbc?state=1", function (re) {
        var pbcList = re.data;
        var form = window.form_new_product;
        var pbcOptions = "";
        for (var i in pbcList) {
            var pbc = pbcList[i];
            pbcOptions += "<option value='" + pbc.id + "'>1×" + pbc.pcount + "</option>";
        }
        $(window.form_new_product.pbcId).append(pbcOptions);
        $(window.form_edit_product.pbcId).append(pbcOptions);
    });
});


// 商品详情
$('#md_edit_company').on('hide.bs.modal', function (event) {
    var form = window.form_edit_product;
    form.reset();
});
$('#md_edit_product').on('show.bs.modal', function (event) {
    var _e = $(event.relatedTarget);
    var productNumber = _e.data("product-number");
    var modal = $(this);
    var form = window.form_edit_product;
    $.get("/product/" + productNumber, function (re) {
        var product = re.data;
        form.id.value = product.id;
        form.name.value = product.name;
        form.number.value = product.number;
        form.barcode.value = product.barcode;
        form.price.value = product.price || "";

        $(form.state).val(product.state);
        $(form.brandId).val(product.brandId);
        $(form.pbcId).val(product.pbcId);

        $("#ctn_edit_product_pics").empty();
        if (product.pictures) {
            var pictures = product.pictures.split(",");
            for (var i in pictures) {
                var uri = pictures[i];
                if (/.+/.test(uri)) {
                    var $imgEle = $("<img src='" + PicDomain + uri + "' data-picuri='" + uri + "'/>");
                    $imgEle.click(function () {
                        $(this).remove();
                    });
                    $("#ctn_edit_product_pics").append($imgEle);
                }
            }
        }
    });
});


//数据导出
$("[name='btn_export']").click(function () {
    var keyword = $("[name='keyword']").val();
    var state = $("[name='state']").val();
    
    
    //有数据时才能导出
    if($("#resultTotal_hidden").val() != 0){
    	bootbox.bconfirm("确定是否导出？", function (re) {
            if (re) {
            	 var url="/product/export?keyword="+keyword+"&state="+state;
                 window.open(url);
            }
        });
    }else{
    	bootbox.balert("没有数据，无法导出!");
    }
});
